package hhpoll

class Poll {

    User administrator
    String title
    Date createDate = new Date()
    Date startDate = new Date()
    Date endDate = new Date()
    Boolean _public
    Boolean dynamic
    Boolean anonymous

    static hasMany = [participants:PollParticipant, items:PollItem]
    static constraints = {
        administrator()
        title(maxSize:80,blank:false)
        // customer validators explained in grails-getting-started.pdf
        startDate(validator: {return (it > new Date())})
        createDate(validator: {return (it > new Date())})
        // custom validator per this URL:
        // http://stackoverflow.com/questions/4081169/grails-min-constraint-for-date-validation
        endDate(validator: { val, obj ->
            val?.after(obj.startDate)
        })
        // Limit our participants to 100 for now.
        participants(maxSize:100)
        // Limit items to 15 based on our requirements.
        items(maxSize:15,
              validator: { val, obj ->
            return obj?.dynamic ? true : val?.size > 0
        })
        // Do we want to allow a poll to be created without any participants?
    }

    String toString() {
        "${this.id}: ${this.title}"
    }
}
